
#include "Allegro_initialize.h"
#include "game.h"
using namespace std;





int main(void)
{
	//------------------------------------------
	//Initialise the Resource Stack
	//------------------------------------------
	
	Allegro_resources resources = {0,0,0};

	SETUP_STATUS  status = init_alleg(resources);
	string error_message = create_error_message(status);
	
	if (status != SETUP_SUCCESFULL)
	{
		display_setup_error_message(error_message);
	}
	//------------------------------------------

	//------------------------------------------
	//Declare Local Variables
	//------------------------------------------
	//Initiate the Game Object (Contains all other objects!)
	Game thisgame;
	
	//Set up the user input keys
	bool arrow_keys[4] = {false, false, false, false};
	bool shoot_key = false; 
	Direction dir = NONE;

	//Define loop and rendering booleans
	bool done = false;
	bool redraw = true;
	bool toggle = false;
	//------------------------------------------
	//Enter the Game loop
	//------------------------------------------
	al_start_timer(resources._timer);
	while(!done)
	{
		ALLEGRO_EVENT ev;
		al_wait_for_event(resources._event_queue, &ev);

		if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			switch(ev.keyboard.keycode)
			{
				case ALLEGRO_KEY_UP:
					arrow_keys[UP] = true;
					dir = UP;
					break;
				case ALLEGRO_KEY_DOWN:
					arrow_keys[DOWN] = true;
					dir = DOWN;
					break;
				case ALLEGRO_KEY_RIGHT:
					arrow_keys[RIGHT] = true;
					dir = RIGHT;
					break;
				case ALLEGRO_KEY_LEFT:
					arrow_keys[LEFT] = true;
					dir = LEFT;
					break;
				case ALLEGRO_KEY_SPACE:
					shoot_key = true;
					break;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_UP)
		{
			
			switch(ev.keyboard.keycode)
			{
				case ALLEGRO_KEY_UP:
					arrow_keys[UP] = false;
					break;
				case ALLEGRO_KEY_DOWN:
					arrow_keys[DOWN] = false;
					break;
				case ALLEGRO_KEY_RIGHT:
					arrow_keys[RIGHT] = false;
					break;
				case ALLEGRO_KEY_LEFT:
					arrow_keys[LEFT] = false;
					break;
				case ALLEGRO_KEY_SPACE:
					shoot_key = false;
					break;
				case ALLEGRO_KEY_ESCAPE:
					done = true;
					break;
				
			}
		}
		else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			done = true;
		}
		else if(ev.type == ALLEGRO_EVENT_TIMER)
		{
		
			if(toggle)
			{
				if(!arrow_keys[0]&&!arrow_keys[1]&&!arrow_keys[2]&&!arrow_keys[3]) 
				{
					dir = NONE;
				}
				thisgame.update(dir);
				toggle = !toggle;
			}
			
			redraw = true;

		}

		if(redraw && al_is_event_queue_empty(resources._event_queue))
		{
			redraw = false;
			toggle = !toggle;
			
			//----------------------------
			//------------------------------------------------------------
		//DRAW ROUTINE
		//Converts the <_screen_map> and objects into a drawn result
		//------------------------------------------------------------	
		//Step 1: Draw the sand
			 
			//Make the sand
			int pixel_ratio = _game_box_size/_game_grid_elements;
			ALLEGRO_BITMAP *sand = al_create_bitmap(pixel_ratio,pixel_ratio);
			al_set_target_bitmap(sand);
			al_clear_to_color(al_map_rgb(255,215,0));
			al_draw_filled_rectangle(1,1,3,3,al_map_rgb(218,165,32));

			al_set_target_bitmap(al_get_backbuffer(resources._display));

			for(int i = 0; i < _game_grid_elements; i++)
			{
				for(int j = 0; j < _game_grid_elements; j++)
				{
					if(thisgame.is_sand_at(i,j))
					{
						al_draw_bitmap(sand, _game_box_start_x + i*pixel_ratio,  _game_box_start_y + j*pixel_ratio, 0);
						//al_draw_filled_rectangle(_game_box_start_x + i*pixel_ratio,  _game_box_start_y + j*pixel_ratio, _game_box_start_x + i*pixel_ratio +5,  _game_box_start_y + j*pixel_ratio +5, al_map_rgb(0,255,0)); 
					}
				}
			}

		//Step 2: Draw the objects
			
		al_set_target_bitmap(al_get_backbuffer(resources._display));

			int object_x;
			int object_y;
			int object_border;
			int object_count; 
			ALLEGRO_BITMAP *object_bitmap;
		

			for (int i = 0; i < thisgame.get_number_of_objects(); i++)
			{
				object_border = thisgame.get_object_border(i);
				object_x = thisgame.get_object_x(i) - object_border;
				object_y = thisgame.get_object_y(i);
				object_y = thisgame.get_object_y(i) - object_border;
				object_border = thisgame.get_object_border(i);
				object_border = object_border*2 +1;
				object_bitmap = thisgame.get_object_bitmap(i);
				int height = al_get_bitmap_height(object_bitmap);
				int width = al_get_bitmap_width(object_bitmap);

				ALLEGRO_BITMAP *object_bitmap2 = al_create_bitmap(object_border*pixel_ratio,object_border*pixel_ratio);
				al_set_target_bitmap(object_bitmap2);
				al_draw_scaled_bitmap(object_bitmap, 0,0, width, height, 0,0, object_border*pixel_ratio, object_border*pixel_ratio,0);
				al_set_target_bitmap(al_get_backbuffer(resources._display));
				al_draw_bitmap(object_bitmap2, _game_box_start_x + object_x*pixel_ratio,  _game_box_start_y +  object_y*pixel_ratio, 0);
			}
			//-----------------------------------------------------------


			al_flip_display();
			al_clear_to_color(al_map_rgb(0,0,0));




		//------------------------------------------------------------
		//------------------------------------------------------------

		}
	}


	al_destroy_event_queue(resources._event_queue);
	al_destroy_timer(resources._timer);
	al_destroy_display(resources._display);
	
	return 0;
}


